﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using WTAppletProjectHTTPS.Models;

namespace WTAppletProjectHTTPS.Controllers
{
    [JwtAuthentication]
    public class ColaFirstPiecePatrolInspectionRecordController : ApiController
    {
        private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
        public class ErrorMessage { 
            public string CFPIR_Inspector { get; set; }
            public string CFPIR_Shift { get; set; }
            public string CFPIR_LineParting { get; set; }

            public string CFPIR_ProductModel { get; set; }

            public string CFPIR_OrderNumber { get; set; }
        }
        // POST api/<controller>
        public async Task<HttpResponseMessage> Post([FromBody] ColaFirstPiecePatrolInspectionRecord CFPIR)
        {
            ReturnObject RO = new ReturnObject();
           
            if (CFPIR == null ||
                string.IsNullOrEmpty(CFPIR.CFPIR_Inspector) ||
                string.IsNullOrEmpty(CFPIR.CFPIR_Shift) ||
                string.IsNullOrEmpty(CFPIR.CFPIR_LineParting) ||
                string.IsNullOrEmpty(CFPIR.CFPIR_ProductModel) ||
                string.IsNullOrEmpty(CFPIR.CFPIR_OrderNumber))
            {

                
                
                Logger.Warn("输入值无效");
                RO.Status = HttpStatusCode.BadRequest;
                RO.Message = "输入值无效";
                RO.Data = CFPIR;
                return Request.CreateResponse(HttpStatusCode.BadRequest, RO);
            }

            using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionName"].ConnectionString))
            {
                connection.Open();
                using (var transaction = connection.BeginTransaction())
                {
                    try
                    {
                        var query = @"INSERT INTO ColaFirstPiecePatrolInspectionRecord 
                                    (CFPInspector, CFPShift, CFPInspectionTime, CFPLineParting, CFPInspectionTiming, 
                                     CFPProductModel, CFPOrderNumber, CFPInspectionItem, CFPProductMarking, 
                                     CFPProductAppearance, CFPProductAssemblyInspection, CFPProcessPersonnelQualification, 
                                     CFPProcessEquipmentTooling, CFPProcessMaterialConfirmation, CFPProcessMethodConfirmation, 
                                     CFPProductPerformance, CFPProductContinuityTest, CFPProductHandfeel, CFPProductPackagingInspection, 
                                     CFPProductBarcodeScan, CFPFinalConclusion, CFPWhichInspectionQualified, 
                                     CFPNonconformanceDescription1, CFPNonconformanceImage1, 
                                     CFPNonconformanceDescription2, CFPNonconformanceImage2, 
                                     CFPNonconformanceDescription3, CFPNonconformanceImage3,
                                     CFPNonconformanceDescription4, CFPNonconformanceImage4,
                                     CFPCreateCreator,CFPCreateTime) 
                                    VALUES 
                                    (@Inspector, @Shift, @InspectionTime, @LineParting, @InspectionTiming, 
                                     @ProductModel, @OrderNumber, @InspectionItem, @ProductMarking, 
                                     @ProductAppearance, @ProductAssemblyInspection, @ProcessPersonnelQualification, 
                                     @ProcessEquipmentTooling, @ProcessMaterialConfirmation, @ProcessMethodConfirmation, 
                                     @ProductPerformance, @ProductContinuityTest, @ProductHandfeel, @ProductPackagingInspection, 
                                     @ProductBarcodeScan, @FinalConclusion, @WhichInspectionQualified, 
                                     @NonconformanceDescription1, @NonconformanceImage1, 
                                     @NonconformanceDescription2, @NonconformanceImage2, 
                                     @NonconformanceDescription3, @NonconformanceImage3,
                                     @NonconformanceDescription4, @NonconformanceImage4,
                                     @CreateCreator,@CreateTime);
                                    SELECT CAST(scope_identity() AS int);";

                        SqlParameter[] parameters = {
                            new SqlParameter("@Inspector", SqlDbType.NVarChar, 50) { Value = CFPIR.CFPIR_Inspector },
                            new SqlParameter("@Shift", SqlDbType.NVarChar, 50) { Value = CFPIR.CFPIR_Shift },
                            new SqlParameter("@InspectionTime", SqlDbType.DateTime) { Value = CFPIR.CFPIR_InspectionTime },
                            new SqlParameter("@LineParting", SqlDbType.NVarChar, 50) { Value = CFPIR.CFPIR_LineParting },
                            new SqlParameter("@InspectionTiming", SqlDbType.NVarChar, 50) { Value = CFPIR.CFPIR_InspectionTiming },
                            new SqlParameter("@ProductModel", SqlDbType.NVarChar, 50) { Value = CFPIR.CFPIR_ProductModel },
                            new SqlParameter("@OrderNumber", SqlDbType.NVarChar, 50) { Value = CFPIR.CFPIR_OrderNumber },
                            new SqlParameter("@InspectionItem", SqlDbType.NVarChar, 50) { Value = CFPIR.CFPIR_InspectionItem },
                            new SqlParameter("@ProductMarking", SqlDbType.NVarChar, 50) { Value = CFPIR.CFPIR_ProductMarking },
                            new SqlParameter("@ProductAppearance", SqlDbType.NVarChar, 50) { Value = CFPIR.CFPIR_ProductAppearance },
                            new SqlParameter("@ProductAssemblyInspection", SqlDbType.NVarChar, 50) { Value = CFPIR.CFPIR_ProductAssemblyInspection },
                            new SqlParameter("@ProcessPersonnelQualification", SqlDbType.NVarChar, 50) { Value = CFPIR.CFPIR_ProcessPersonnelQualification },
                            new SqlParameter("@ProcessEquipmentTooling", SqlDbType.NVarChar, 50) { Value = CFPIR.CFPIR_ProcessEquipmentTooling },
                            new SqlParameter("@ProcessMaterialConfirmation", SqlDbType.NVarChar, 50) { Value = CFPIR.CFPIR_ProcessMaterialConfirmation },
                            new SqlParameter("@ProcessMethodConfirmation", SqlDbType.NVarChar, 50) { Value = CFPIR.CFPIR_ProcessMethodConfirmation },
                            new SqlParameter("@ProductPerformance", SqlDbType.NVarChar, 50) { Value = CFPIR.CFPIR_ProductPerformance },
                            new SqlParameter("@ProductContinuityTest", SqlDbType.NVarChar, 50) { Value = CFPIR.CFPIR_ProductContinuityTest },
                            new SqlParameter("@ProductHandfeel", SqlDbType.NVarChar, 50) { Value = CFPIR.CFPIR_ProductHandfeel },
                            new SqlParameter("@ProductPackagingInspection", SqlDbType.NVarChar, 50) { Value = CFPIR.CFPIR_ProductPackagingInspection },
                            new SqlParameter("@ProductBarcodeScan", SqlDbType.NVarChar, 50) { Value = CFPIR.CFPIR_ProductBarcodeScan },
                            new SqlParameter("@FinalConclusion", SqlDbType.NVarChar, 50) { Value = CFPIR.CFPIR_FinalConclusion },
                            new SqlParameter("@WhichInspectionQualified", SqlDbType.NVarChar, 50) { Value = CFPIR.CFPIR_WhichInspectionQualified },
                            new SqlParameter("@NonconformanceDescription1", SqlDbType.NVarChar, 500) { Value = (object)CFPIR.CFPIR_NonconformanceDescription1 ?? DBNull.Value },
                            new SqlParameter("@NonconformanceImage1", SqlDbType.NVarChar, 1000) { Value = (object)CFPIR.CFPIR_NonconformanceImage1 ?? DBNull.Value },
                            new SqlParameter("@NonconformanceDescription2", SqlDbType.NVarChar, 500) { Value = (object)CFPIR.CFPIR_NonconformanceDescription2 ?? DBNull.Value },
                            new SqlParameter("@NonconformanceImage2", SqlDbType.NVarChar, 1000) { Value = (object)CFPIR.CFPIR_NonconformanceImage2 ?? DBNull.Value },
                            new SqlParameter("@NonconformanceDescription3", SqlDbType.NVarChar, 500) { Value = (object)CFPIR.CFPIR_NonconformanceDescription3 ?? DBNull.Value },
                            new SqlParameter("@NonconformanceImage3", SqlDbType.NVarChar, 1000) { Value = (object)CFPIR.CFPIR_NonconformanceImage4 ?? DBNull.Value },
                            new SqlParameter("@NonconformanceDescription4", SqlDbType.NVarChar, 500) { Value = (object)CFPIR.CFPIR_NonconformanceDescription3 ?? DBNull.Value },
                            new SqlParameter("@NonconformanceImage4", SqlDbType.NVarChar, 1000) { Value = (object)CFPIR.CFPIR_NonconformanceImage4 ?? DBNull.Value },
                            new SqlParameter("@CreateCreator", SqlDbType.NVarChar, 50) { Value = CFPIR.CFPIR_CreateCreator },
                            new SqlParameter("@CreateTime", SqlDbType.DateTime) { Value = DateTime.Now }
                        };

                        using (var command = new SqlCommand(query, connection, transaction))
                        {
                            command.Parameters.AddRange(parameters);
                            var result = await command.ExecuteScalarAsync();
                            if (result != null)
                            {
                                transaction.Commit();
                                CFPIR.CFPIR_ID = Convert.ToInt32(result);
                                Logger.Info("新增成功");
                                RO.Status = HttpStatusCode.OK;
                                RO.Message = "OK";
                                RO.Data = CFPIR;
                                return Request.CreateResponse(HttpStatusCode.OK, RO);
                            }
                            else
                            {
                                transaction.Rollback();
                                Logger.Error("插入记录失败");
                                RO.Status = HttpStatusCode.InternalServerError;
                                RO.Message = "Error";
                                RO.Data = null;
                                return Request.CreateResponse(HttpStatusCode.InternalServerError, RO);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        Logger.Warn("新增失败: " + ex.Message);
                        RO.Status = HttpStatusCode.InternalServerError;
                        RO.Message = "Error" + ex.Message;
                        RO.Data = null;
                        return Request.CreateResponse(HttpStatusCode.InternalServerError, RO);
                    }
                }
            }
        }

    }
}